#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
	int t, m, v, w, i, j, s[1001];

	memset(s, 0, sizeof(s));
	scanf("%d %d", &t, &m);
	for (i=0; i<m; i++) {
		scanf("%d %d", &v, &w);
		for (j=t; j>=v; j--) {
			if (s[j-v] + w > s[j]) {
				s[j] = s[j-v] + w;
			}
		}
	}

	printf("%d\n", s[t]);

	return 0;
}
